package com.sp.fresh_produce.controller;

import com.sp.fresh_produce.common.GenericReturnApiObject;
import com.sp.fresh_produce.controller.dto.req.RequestCategory;
import com.sp.fresh_produce.controller.dto.VO.CategoryVORequest;
import com.sp.fresh_produce.service.CategoryService;
import com.sp.fresh_produce.web.AdminRequired;
import com.sp.fresh_produce.model.pojo.Category;
import com.sp.fresh_produce.ex.ExceptionCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 商品分类
 *
 */
@RestController
@RequestMapping("/category")
@Tag(name = "Category", description = "商品分类接口")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;
    /**
     *  新增商品分类
     */
    @PostMapping("/admin/addCategory")
    @AdminRequired
    @Operation(summary = "新增商品分类")
    public GenericReturnApiObject addCategory(@Valid @RequestBody RequestCategory requestCategory) {
        Category category = new Category();
        BeanUtils.copyProperties(requestCategory, category);
        int rows = categoryService.addCategory(category);
        return resultResponse(rows, ExceptionCodeEnum.UNKNOWN_ERROR);
    }

    /**
     *
     * 删除商品分类目录
     */
    @RequestMapping(value = "/admin/deleteCategory", method = {RequestMethod.GET, RequestMethod.DELETE})
    @AdminRequired
    @Operation(summary = "删除商品分类目录")
    public GenericReturnApiObject deleteCategory(@RequestParam Integer id) {
        int rows = categoryService.deleteCategory(id);
        return resultResponse(rows, ExceptionCodeEnum.DELETE_CATEGORY_FAILED);
    }

    /**
     *  商品目录分页查询
     */
    @GetMapping("/admin/getCategoryList")
    @AdminRequired
    @Operation(summary = "商品目录分页查询")
    public  GenericReturnApiObject getCategoryListForAdmin(@RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        return success(categoryService.getCategoryList(pageNum, pageSize));
    }

    /**
     *  前台商品分类目录查询
     */
    @GetMapping("/getCategoryList")
    @Operation(summary = "前台商品分类目录查询(用户看的)")
    public GenericReturnApiObject getCategoryListForUser(Integer parentId) {
        List<CategoryVORequest> categoryList = categoryService.getCategoryListForUser(0);
        return success(categoryList);
    }
}
